import os
import datetime
from flask import (Blueprint,
                   request,
                   render_template,
                   flash,
                   redirect,
                   url_for,
                   send_from_directory,
                   make_response
                   )
from .forms import UploadForm
from data_folder_old import HandleComInfo
import config

bp = Blueprint('data_script_old', __name__, url_prefix="/script_old")


@bp.route("/", methods=["GET", 'POST'])
def uploads():
    if request.method == "GET":
        return render_template("com_handle_output_old.html")
    else:
        form = UploadForm(request.files)
        if form.validate():
            file = form.file.data
            # filename = f'企业表处理_{datetime.datetime.now().strftime("%Y%m%d")}.xlsx'
            filename = f'企业表处理.xlsx'
            file.save(os.path.join(config.UPLOAD_FOLDER_OLD, filename))
            hci = HandleComInfo()
            hci.general_handle()
            hci.xxz_handle()
            hci.job51_handle()
            flash("上传成功！")
            files = ['点击下载校校招导出企业', '点击下载人才网导出企业']
            return render_template("com_handle_output_old.html", files=files)

        else:
            flash("上传失败！")
            return redirect(url_for("data_script_old.uploads"))


@bp.route("/down/xxz")
def down_xxz():
    """下载校校招"""
    return make_response(send_from_directory(r'data_folder_old', 'xxz.xlsx'))


@bp.route("/down/51job")
def down_51job():
    """下载人才网"""
    return make_response(send_from_directory(r'data_folder_old', '51job.xlsx'))


@bp.route("/down/demo", methods=["GET"])
def down_demo():
    """下载样例"""
    return make_response(send_from_directory(r'data_folder_old', '企业表处理(样例).xlsx'))


@bp.route("/down/fill_type")
def fill_type():
    """根据岗位名称填充岗位类别信息"""
    hci = HandleComInfo()
    hci.fill_type()
    return make_response(send_from_directory(r'data_folder_old', 'xxz.xlsx'))

